﻿using System.Windows.Controls;
using FishSimulation.Model;
using System.Collections.Generic;
using System.Windows;

namespace FishSimulation.Views {
    /// <summary>
    /// Interaction logic for FishGridView.xaml
    /// </summary>
    public partial class ActorGridView : ViewBase {
        public ActorGridView() {
            InitializeComponent();
            this.Bind();
        }

        private void btnSave_Click(object sender, System.Windows.RoutedEventArgs e) {
            Repository.Instance.Save();
        }

        private void dg_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Delete)
            {
                if (dg.SelectedItems != null && this.Confirm("Really delete selected actors?"))
                {
                    List<Actor> alist = new List<Actor>();
                    foreach (var obj in dg.SelectedItems)
                    {
                        Actor a = obj as Actor;
                        if (a != null) {
                            alist.Add(a);
                        }
                    }
                    Repository.Instance.Delete(alist.ToArray());                    
                }
            }
        }

        private void Bind() {
            dg.ItemsSource = Repository.Instance.World.Actors;
        }                
    }
}
